package br.ufba.lasid.jitclouds.mda;

import br.ufba.lasid.jitclouds.mda.api.JiTCommunicatorAPI;
import br.ufba.lasid.jitclouds.mda.api.JiTConfigurationAPI;
import br.ufba.lasid.jitclouds.mda.api.JiTFailureDetectorAPI;
import br.ufba.lasid.jitclouds.mda.api.JiTReplicationManagerAPI;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * JiTContext is a Facade for the basic software components of the MDA.
 *
 */
public final class JiTContext {

    private static JiTCommunicatorAPI Communicator = null;
    private static JiTFailureDetectorAPI FailureDetector = null;
    private static JiTReplicationManagerAPI ReplicationManager = null;
    private static JiTConfigurationAPI Configuration = null;
    private static final String Config = "config.mda.properties";
    private static final Log Logger = LogFactory.getLog(JiTContext.class);

    private JiTContext() {
    }

    public static JiTConfigurationAPI getConfiguration() throws Exception {

        if (JiTContext.Configuration == null) {
            JiTContext.Configuration = new JiTConfiguration(Config);
            ((JiTConfiguration) JiTContext.Configuration).setAutoSave(true);
        }

        return JiTContext.Configuration;
    }

    public static void setConfiguration(JiTConfigurationAPI newConfiguration) throws Exception {
        if (JiTContext.Configuration != null) {
            JiTContext.Configuration = newConfiguration;
        }
    }

    public static void setFailureDetector(JiTFailureDetectorAPI newFailureDetector) throws Exception {
        if (JiTContext.FailureDetector != null) {
            JiTContext.FailureDetector = newFailureDetector;
        }
    }
    
    public static void setReplicationManager(JiTReplicationManagerAPI newReplicationManager) throws Exception {
        if (JiTContext.ReplicationManager != null) {
            JiTContext.ReplicationManager = newReplicationManager;
        }
    }

    public static void setCommunicator(JiTCommunicatorAPI newCommunicator) throws Exception {
        if (JiTContext.Communicator != null) {
            JiTContext.Communicator = newCommunicator;
        }
    }

    public static JiTFailureDetectorAPI getFailureDetector() throws Exception {
        if (JiTContext.FailureDetector == null) {
            JiTContext.FailureDetector = new JiTFailureDetector();
            JiTContext.FailureDetector.deploy();
        }

        return JiTContext.FailureDetector;
    }

    public static JiTReplicationManagerAPI getReplicationManager() throws Exception {
        if (JiTContext.ReplicationManager == null) {
            JiTContext.ReplicationManager = new JiTReplicationManager();
            JiTContext.ReplicationManager.deploy();
        }

        return JiTContext.ReplicationManager;
    }

    public static JiTCommunicatorAPI getCommunicator() throws Exception {
        if (JiTContext.Communicator == null) {
            JiTContext.Communicator = new JiTCommunicator();
        }

        return JiTContext.Communicator;
    }

    public static void main(String[] args) throws Exception {
        //nothing to do yet.
    }
}
